package org.zky.modules.transaction.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zky.aop.log.Log;
import org.zky.modules.transaction.domain.ZkyOrderTab;
import org.zky.modules.transaction.service.ZkyOrderTabService;
import org.zky.modules.transaction.service.dto.ZkyOrderTabQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author chenglong
* @date 2020-03-23
*/
@Api(tags = "order管理")
@RestController
@RequestMapping("/api/zkyOrderTab")
public class ZkyOrderTabController {

    private final ZkyOrderTabService zkyOrderTabService;

    public ZkyOrderTabController(ZkyOrderTabService zkyOrderTabService) {
        this.zkyOrderTabService = zkyOrderTabService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zkyOrderTab:list')")
    public void download(HttpServletResponse response, ZkyOrderTabQueryCriteria criteria) throws IOException {
        zkyOrderTabService.download(zkyOrderTabService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询order")
    @ApiOperation("查询order")
    @PreAuthorize("@el.check('zkyOrderTab:list')")
    public ResponseEntity<Object> getZkyOrderTabs(ZkyOrderTabQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(zkyOrderTabService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增order")
    @ApiOperation("新增order")
    @PreAuthorize("@el.check('zkyOrderTab:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ZkyOrderTab resources){
        return new ResponseEntity<>(zkyOrderTabService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改order")
    @ApiOperation("修改order")
    @PreAuthorize("@el.check('zkyOrderTab:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ZkyOrderTab resources){
        zkyOrderTabService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除order")
    @ApiOperation("删除order")
    @PreAuthorize("@el.check('zkyOrderTab:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        zkyOrderTabService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}